<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('收款流水') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('收款公司')" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          :placeholder="$t('收款公司')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('项目名称')" prop="actTitle">
        <el-input
          v-model="queryParams.dealName"
          :placeholder="$t('项目名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('交易流水号')" prop="tradeNumber">
        <el-input
          v-model="queryParams.tradeNumber"
          :placeholder="$t('交易流水号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('交易日期')" prop="tradeTime">
        <el-date-picker
          v-model="queryParams.tradeTime"
          clearable
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('交易日期')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:collectionFlow:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          v-hasPermi="['deal:collectionFlow:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}</el-button> -->
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="collectionFlowList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('收款公司')" align="center" prop="companyName" />
      <el-table-column :label="$t('交易流水号')" align="center" prop="tradeNumber" />
      <el-table-column :label="$t('项目名称')" align="center" prop="dealName" />
      <el-table-column :label="$t('项目类型')" align="center" prop="dealType" />
      <el-table-column :label="$t('交易日期')" align="center" prop="tradeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tradeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('交易金额（万）')" align="center" prop="tradeMoney" width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.tradeMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('对方户名')" align="center" prop="oppositeAccountName" />
      <el-table-column :label="$t('用途')" align="center" prop="purpose" />
      <el-table-column :label="$t('认领人')" align="center" prop="claimByName" />
      <el-table-column :label="$t('状态')" align="center" prop="status" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="showHandle(scope.row)"
          >{{ $t('查看') }}</el-button>
          <el-button
            v-if="scope.row.status!=='已认领'"
            v-hasPermi="['deal:collectionFlow:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-if="scope.row.status!=='已认领'"
            v-hasPermi="['deal:collectionFlow:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="claimHandle(scope.row)"
          >{{ $t('认领') }}</el-button>
          <el-button
            v-if="scope.row.status!=='未认领'"
            v-hasPermi="['deal:collectionFlow:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="cancelClaimHandle(scope.row)"
          >{{ $t('取消认领') }}</el-button>
          <el-button
            v-if="scope.row.status!=='已认领'"
            v-hasPermi="['deal:collectionFlow:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收款流水对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <collectionFlowForm ref="collectionFlowForm" />
    </el-dialog>
    <!-- 认领收款流水对话框 -->
    <el-dialog :title="$t(claimtitle)" :visible.sync="formOpenClaim" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealDistributionFormClaim ref="dealDistributionFormClaim" />
    </el-dialog>
  </div>
</template>

<script>
import { listCollectionFlow, delCollectionFlow, exportCollectionFlow, cancelClaim } from '@/api/deal/collectionFlow/collectionFlow'
import collectionFlowForm from './collectionFlowForm.vue'
import dealDistributionFormClaim from '../distribution/dealDistributionClaimForm.vue'
export default {
    components: {
        collectionFlowForm,
        dealDistributionFormClaim
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 收款流水表格数据
          collectionFlowList: [],
          // 弹出层标题
          title: '收款流水',
          claimtitle: '项目回款',
          // 是否显示弹出层
          formOpen: false,
          formOpenClaim: false,
          // 查询参数
          queryParams: {
              companyName: undefined,
              dealName: undefined,
              tradeNumber: undefined,
              tradeTime: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询收款流水列表 */
    getList() {
        this.loading = true
        listCollectionFlow(this.queryParams).then(response => {
            this.collectionFlowList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            flowId: undefined,
            actId: undefined,
            actTitle: undefined,
            actStatus: '0',
            claimBy: undefined,
            claimTime: undefined,
            dealId: undefined,
            dealName: undefined,
            dealType: undefined,
            distributionId: undefined,
            companyNumber: undefined,
            tradeNumber: undefined,
            tradeWay: undefined,
            tradeTime: undefined,
            tradeCurrency: undefined,
            tradeMoney: undefined,
            cnyMoney: undefined,
            myAccountNumber: undefined,
            myAccountName: undefined,
            openBankMark: undefined,
            accountMark: undefined,
            openBankNumber: undefined,
            openBankName: undefined,
            oppositeAccountNumber: undefined,
            oppositeAccountName: undefined,
            oppositeOpenBankNumber: undefined,
            oppositeOpenBankName: undefined,
            purpose: undefined,
            companyName: undefined,
            userName: undefined,
            flowStatusRemark: undefined,
            accountSubject: undefined,
            tradeRecordStatus: '0',
            status: '0',
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.collectionFlowForm.init(null, true)
        })
    },
    /** 查看按钮操作 */
    showHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.collectionFlowForm.init(row, false)
        })
    },
    /** 认领按钮操作 */
    claimHandle(row) {
        this.formOpenClaim = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealDistributionFormClaim.init(row)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.collectionFlowForm.init(row, true)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delCollectionFlow(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 取消认领按钮操作 */
    cancelClaimHandle(row) {
      cancelClaim(row.id).then(response => {
          console.log(response)
          this.getList()
          this.msgSuccess(this.$t('操作 成功'))
          this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportCollectionFlow(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    }
  }
}
</script>
